package com.neulogistics.core.substation.infrastructure.persistence.converter;

import cn.hutool.json.JSONUtil;

import com.neulogistics.core.substation.domain.model.aggregate.Substation;
import com.neulogistics.core.substation.domain.model.valueobject.Address;
import com.neulogistics.core.substation.infrastructure.persistence.orm.po.SubstationPO;
import org.springframework.beans.BeanUtils;

public class SubstationEntityPOConverter {
    public static SubstationPO entityToPO(Substation substation){
        if(substation == null)
            return null;

        SubstationPO substationPO = new SubstationPO();
        // 复制普通属性
        BeanUtils.copyProperties(substation, substationPO);
        // 地址值对象转换为json对象
        if(substation.getSubstationAddress() != null){
            substationPO.setSubstationAddress(JSONUtil.parseObj(substation.getSubstationAddress()));
        }
        return substationPO;
    }

    public static Substation POToEntity(SubstationPO substationPO){
        if(substationPO == null)
            return null;

        Substation substation = new Substation();
        // 复制普通属性, id是可以在这里被复制进去的
        BeanUtils.copyProperties(substationPO, substation);
        // json对象转换为值对象
        substation.setSubstationAddress(JSONUtil.toBean(substationPO.getSubstationAddress(), Address.class));
        return substation;
    }

}
